package com.flower.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.flower.domain.ApiResult;
import com.flower.domain.entity.productSpecs.AttributeValue;
import com.flower.service.AttributeValueService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Slf4j
@RequestMapping("/attributeValue")
public class BackageAttributeValueController {

    @Resource
    private AttributeValueService attributeValueService;

    /**
     * 添加或编辑一个sku值
     * @param attributeValue
     * @return
     */
    @PostMapping("/addAndup")
    public ApiResult<Boolean> addupAttributeValue(@RequestBody AttributeValue attributeValue){
        //判断是否有id值 没有则是添加
        if(attributeValue.getId()== null){
            attributeValueService.save(attributeValue);
        }else {
            LambdaUpdateWrapper<AttributeValue> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(AttributeValue::getId,attributeValue.getId());
            wrapper.set(AttributeValue::getAttributeValue,attributeValue.getAttributeValue());
            attributeValueService.update(wrapper);
        }
        return ApiResult.ok();
    }

    /**
     * 删除属性值
     * @param attributeValueId
     * @return
     */
    @GetMapping("/delete/{attributeValueId}")
    public ApiResult<Boolean> attributeValueDel(@PathVariable int attributeValueId){
       attributeValueService.removeById(attributeValueId);
       return ApiResult.ok();
    }

}
